﻿using Microsoft.EntityFrameworkCore;
using ReportingSystem.IRepository;
using ReportingSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReportingSystem.Repository
{
    public class RepairsRepository : IRepairsRepository
    {
        private readonly AppDbContext _context;

        public RepairsRepository(AppDbContext context)
        {
            _context = context;
        }

        public void Create(Repairs entity)
        {
            _context.Repairss.Add(entity);
            _context.SaveChanges();
        }

        public List<Repairs> GetAll()
        {
           return  _context.Repairss.Include(x=>x.Users).ToList();
        }

        public Repairs Get(int id)
        {
            return _context.Repairss.Where(o=>o.ID==id).Include(x => x.Users).FirstOrDefault();
        }

        public List<Repairs> GetAll(dynamic repairs)
        {
            List<Repairs> data = new List<Repairs>();
            try
            {
                DateTime beginTime = repairs.beginTime.Value;
                if (false)
                {
                    data = _context.Repairss.Where(o => o.beginTime == beginTime).Include(x => x.Users).ToList();
                }
            }
            catch
            {
                try
                {
                    DateTime endTime = repairs.endTime.Value;
                    if (false)
                    {
                        data = _context.Repairss.Where(o => o.endTime == endTime).Include(x => x.Users).ToList();
                    }
                }
                catch 
                {
                    try
                    {
                        var repairsState = repairs.state.ToString();
                        int state = int.Parse(repairsState);
                        data = _context.Repairss.Where(o => o.state == state).Include(x => x.Users).ToList();
                    }
                    catch { return _context.Repairss.Include(x => x.Users).ToList(); }
                }
                try
                {
                    var repairsState = repairs.state.ToString();
                    int state = int.Parse(repairsState);
                    data = data.Where(o => o.state == state).ToList();
                }
                catch { }
            }
            try
            {
                DateTime endTime = repairs.endTime.Value;
                if (false)
                {
                    data = data.Where(o => o.endTime == endTime).ToList();
                }
            }
            catch { }
            try
            {
                var repairsState = repairs.state.ToString();
                int state = int.Parse(repairsState);
                data = data.Where(o => o.state == state).ToList();
            }
            catch { }
            return data;
        }

        public void Update(Repairs repairs)
        {
            _context.Repairss.Update(repairs);
            _context.SaveChanges();
        }
    }
}
